package com.joysuch.wwyt.alert.trigger;

import com.joysuch.wwyt.alert.AlertListenerConfig;
import com.joysuch.wwyt.api.dataaccess.bean.OrientationAlarmCountBean;
import com.joysuch.wwyt.api.dataaccess.bean.OrientationScreenAreaPersonBean;
import com.joysuch.wwyt.api.dataaccess.bean.OrientationScreenBinderBean;
import com.joysuch.wwyt.bp.entity.BpEnterpriseInfo;
import com.joysuch.wwyt.core.entity.BaseConfig;
import com.joysuch.wwyt.core.enums.BaseConfigCodes;
import com.joysuch.wwyt.mh.websocket.MonitorPointDataWebSocket;
import com.joysuch.wwyt.mh.websocket.WebsocketDataType;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author ZhaoQiuWei
 * @Date 2021/11/8 19:20
 */
public class LocateDataTrigger {
    private static final Logger logger = LoggerFactory.getLogger(LocateDataTrigger.class);

    private static volatile boolean toStop;

    private static final LocateDataTrigger instance = new LocateDataTrigger();

    public static LocateDataTrigger getInstance() {
        return instance;
    }

    private Thread triggerThread;


    /**
     * 开始任务
     */
    public void start() {
        triggerThread = new Thread(() -> {
            while (!toStop) {
                try {
//                    List<OrientationAlarmCountBean> alarmCountList = new ArrayList<>();
                    List<BpEnterpriseInfo> all = AlertListenerConfig.getAlertConfig().getBpEnterpriseInfoDao().findAll();
//                    List<OrientationPageAlarmCertificateOrAreaBean> certificateAlarmList = new ArrayList<>();
//                    List<OrientationPageAlarmCertificateOrAreaBean> areaAlarmList = new ArrayList<>();
                    List<OrientationScreenBinderBean> binderList = new ArrayList<>();
                    List<OrientationScreenAreaPersonBean> areaPersonList = new ArrayList<>();
                    boolean flag = true;
                    for (BpEnterpriseInfo info : all) {
                        if (info.getTenentId() != null && StringUtils.isNotBlank(info.getOrgCode())) {
                            Long tenentId = info.getTenentId();
                            String orgCode = info.getOrgCode().substring(0, 7);
                            BaseConfig config = AlertListenerConfig.getAlertConfig().getBaseConfigService().findConfig(tenentId, orgCode, "locate.version");
                            if (config != null && config.getValue().equals("4.x")) {
                                flag = false;
                            }
                            if (AlertListenerConfig.getAlertConfig().getOrientationDataSyncService().getAlarmSwitch(tenentId, orgCode)) {
                                //报警总数
                                if (flag) {
                                    AlertListenerConfig.getAlertConfig().getOrientationDataSyncService().renewAlarmCount(tenentId, orgCode);
                                }
                                //作业票报警分页
                                // certificateAlarmList.addAll(AlertListenerConfig.getAlertConfig().getOrientationDataSyncService().pageCertificateOrAreaAlarm(tenentId, orgCode, new OrientationSearchBean(1, 100, "pageCertificateAlarm")));
                                //区域人员报警分页
                                // areaAlarmList.addAll(AlertListenerConfig.getAlertConfig().getOrientationDataSyncService().pageCertificateOrAreaAlarm(tenentId, orgCode, new OrientationSearchBean(1, 100, "pageAreaAlarm")));
                                //人员列表
                                binderList.addAll(AlertListenerConfig.getAlertConfig().getOrientationDataSyncService().getScreenBinderData(tenentId, orgCode, "schedule"));
                                //区域人员列表
                                areaPersonList.addAll(AlertListenerConfig.getAlertConfig().getOrientationDataSyncService().getV3ScreenAreaPerson(tenentId, orgCode));
                            }
                        }
                    }

                    // MonitorPointDataWebSocket.sendInfo(certificateAlarmList, WebsocketDataType.LOCATE_CERTIFICATE_ALARM.getIndex());
                    //MonitorPointDataWebSocket.sendInfo(areaAlarmList, WebsocketDataType.LOCATE_AREA_ALRAM.getIndex());
                    MonitorPointDataWebSocket.sendInfo(binderList, WebsocketDataType.LOCATE_BINDER.getIndex());
                    MonitorPointDataWebSocket.sendInfo(areaPersonList, WebsocketDataType.LOCATE_AREA_PERSON.getIndex());
                } catch (Exception e) {
                    logger.error("LocateDataTrigger thread  error:", e);
                }
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    logger.error("LocateDataTrigger thread sleep error:", e);
                }
            }
            logger.info("LocateDataTrigger thread stop");
        });
        triggerThread.setName("LocateDataTrigger");
        triggerThread.setDaemon(true);
        triggerThread.start();
    }


    /**
     * 停止任务
     */
    public void toStop() {
        toStop = true;
        triggerThread.interrupt();
        try {
            triggerThread.join();
        } catch (InterruptedException e) {
            logger.error(e.getMessage(), e);
        }
    }

}
